package com.cloudspace.jindun.adapter.interphone;

import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.model.Person;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ObjUtil;
import com.cloudspace.jindun.utils.PinyinUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zengxianhua on 16/12/25.
 */

public class ChannelAddMemberAdapter extends BaseAdapter {
    private List<Person> datas;
    private FragmentActivity activity;
    private List<Integer> checked;

    private String mIsAlreadyMemberStr;

    private CompoundButton.OnCheckedChangeListener checkLisenter = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView != null || buttonView.getTag() != null){
                Integer position = (Integer) buttonView.getTag();
                if (!ObjUtil.isEmpty(checked) &&  position >= 0 && position < checked.size()){
                    checked.set(position, isChecked?1:0);
                }
            }
        }
    };
    private View.OnClickListener checkClickLisenter = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v != null && v.getTag() != null){
                int position = (int) v.getTag(R.id.position);
                CheckBox cb = (CheckBox) v.getTag();
                cb.setChecked(!cb.isChecked());
                checked.set(position, cb.isChecked() ? 1 : 0);
            }
        }
    };

    public List<Integer> getChecked(){
        return checked;
    }

    public ChannelAddMemberAdapter(FragmentActivity activity){
        this.activity = activity;
    }

    public void setDatas(List<Person> data, String memberStr){
        if (ObjUtil.isEmpty(data)){
            return;
        }

        this.datas = data;
        checked = new ArrayList<Integer>(
                Collections.nCopies(data.size(), 0));

        mIsAlreadyMemberStr = memberStr;

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(datas)){
            return 0;
        }

        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(datas)){
            return null;
        }

        if (position < 0 || position >= datas.size()){
            return null;
        }

        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.item_list_contracts_add, null);
            holder.icon = (ImageView) convertView.findViewById(R.id.phone_image_header);
            holder.name = (TextView) convertView.findViewById(R.id.phone_textview_name);
            holder.firstLetter= (TextView) convertView.findViewById(R.id.firstWord);
            holder.cb_addContact= (CheckBox) convertView.findViewById(R.id.cb_addContact);
            holder.checkly = convertView.findViewById(R.id.checkly);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Person dataBean = datas.get(position);


        holder.name.setText(datas.get(position).getName());
        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.icon, dataBean.getImageUrl(), Configs.IMAGE_CORNER_RADIUS);

        String currentFirstWord = "";
        String preFirstWord = "";
        if (dataBean != null && !TextUtils.isEmpty(dataBean.getName())){
            currentFirstWord = PinyinUtils.getPinyinFirstLetter(dataBean.getName());
        }


        if (!TextUtils.isEmpty(currentFirstWord)){
            if (position == 0) {
                holder.firstLetter.setVisibility(View.VISIBLE);
                holder.firstLetter.setText(currentFirstWord);
            } else {
                //后一个与前一个对比,判断首字母是否相同，相同则隐藏
                preFirstWord = PinyinUtils.getPinyinFirstLetter(datas.get(position - 1).getName());
                if (currentFirstWord.equals(preFirstWord)) {
                    holder.firstLetter.setVisibility(View.GONE);
                } else {
                    holder.firstLetter.setVisibility(View.VISIBLE);
                    holder.firstLetter.setText(currentFirstWord);
                }
         }
        }

        boolean canAdded = canAdded(dataBean.getUserId());

        if (canAdded){
            holder.cb_addContact.setChecked((checked.get(position) == 0)? false:true);
            holder.checkly.setVisibility(View.VISIBLE);
            holder.checkly.setOnClickListener(checkClickLisenter);
            holder.checkly.setTag(R.id.position, position);
            holder.checkly.setTag(holder.cb_addContact);
        }else{
            holder.checkly.setVisibility(View.GONE);
        }

        return convertView;
    }

    private boolean canAdded(String id) {
        boolean isMe = UserSessionManager.getInstance().getUserId().equals(id);
        if (TextUtils.isEmpty(mIsAlreadyMemberStr) || isMe){
            return false;
        }

        return !mIsAlreadyMemberStr.contains("," + id + ",");
    }

    static class ViewHolder {
        ImageView icon;
        TextView name;
        TextView firstLetter;
        CheckBox cb_addContact;
        View checkly;
    }
}